var ClubMemberSettingWindow = Window.extend({
    _ccsFileName: res.ClubMemberSettingWindow_json,
    _blockLayerOpacity: 0,
    // _notCloseBlock: false,
    _userData: null,
    _clubId: null,
    initWindow: function (clubId, userData) {
        this._clubId = clubId;
        this._userData = userData;
        //判断设置的用户是否为管理员
        if (this._userData.admins) {
            this._nodeList['btn_setAdmin'].setVisible(false);
            this._nodeList['btn_unAdmin'].setVisible(true);
        } else {
            this._nodeList['btn_setAdmin'].setVisible(true);
            this._nodeList['btn_unAdmin'].setVisible(false);
        }
        if (gameData.uid != ClubDataManager.getClubData(clubId).owner_uid) {
            //this._nodeList['btn_selectGroup'].setVisible(false);
            this._nodeList['btn_remove'].setPosition(this._nodeList['btn_selectGroup'].getPosition());
            this._nodeList['btn_setPartner'].setVisible(false);
        }
        this._nodeList['btn_jiejin'].setVisible(this._userData.black);
        this._nodeList['btn_jinwan'].setVisible(!this._userData.black);
        if (ClubDataManager.getClubUidIsAdmin(clubId, gameData.uid)) {
            this._nodeList['btn_setAdmin'].setVisible(false);
            this._nodeList['btn_unAdmin'].setVisible(false);
            //this._nodeList['btn_selectGroup'].setVisible(false);
            this._nodeList['btn_remove'].setPosition(this._nodeList['btn_setAdmin'].getPosition());
        }

    },

    _initClickListeners: function () {
        this._clickListeners = {
            'btn_remove': '_clickRemoveMember',
            'btn_jinwan': '_clickJinWan',
            'btn_jiejin': '_clickJieJin',
            'btn_setAdmin': '_clickSetAdmin',
            'btn_unAdmin': '_clickUnAdmin',
            'btn_selectGroup': '_clickSelectGroup',
            'btn_setPartner': '_clickSetPartner'
        }
    },

    _initEventListeners: function () {
        this._eventListeners = {};
        this._eventListeners[ClubGroupSelectEvent] = '_selectGroupHandler';
        this._eventListeners[ClubMsgCode.AddClubGroupUids] = '_addClubGroupUidsHandler';
    },

    /**
     * 设置管理员
     * @private
     */
    _clickSetAdmin: function () {
        var self = this;
        showAlert('是否将玩家设置为管理员?', function () {
            network.send(2103, {cmd: 'setAdmin', club_id: self._clubId, obj_id: self._userData['uid']});
        }, function () {
        });
        this.close();
    },
    /**
     * 设置合伙人
     * @private
     */
    _clickSetPartner: function () {
        var self = this;

        //检测该名下是否有已创建的分组
        var isexistGroup = false;
        var tmpClubInfo = ClubDataManager.getClubData(self._clubId);
        for (var i = 0; i < tmpClubInfo.groups.length; i++){
            if (tmpClubInfo.groups[i].op_uid == self._userData.uid){
                isexistGroup = true;
                break;
            }
        }
        if (isexistGroup == true) return;

        showAlert('是否将玩家设置为合伙人?', function () {
            if (tmpClubInfo.groups && tmpClubInfo.groups.length == 1){
                var clubOwnerId = ClubDataManager.getClubOwnerUid(self._clubId);
                var clubOwnerName = ClubDataManager.getClubOwnerName(self._clubId);
                network.send(2103, {cmd: ClubMsgCode.AddClubGroup, club_id: self._clubId, name: clubOwnerName, op_uid: clubOwnerId, cv: 1});
            }
            network.send(2103, {cmd: ClubMsgCode.AddClubGroup, club_id: self._clubId, name: self._userData['name'], op_uid: self._userData['uid'], cv: 1});
            network.send(2103, {cmd: 'setAdmin', club_id: self._clubId, obj_id: self._userData['uid']});
        }, function () {
        });
        this.close();
    },

    _clickUnAdmin: function () {
        var self = this;
        showAlert('是否将该管理员设置为普通成员?', function () {
            network.send(2103, {cmd: 'unsetAdmin', club_id: self._clubId, obj_id: self._userData['uid']});
        }, function () {
        });
        this.close();
    },

    /**
     * 选择分组
     * @private
     */
    _clickSelectGroup: function () {
        showWindow('ClubGroupWindow', GroupWindowType_Grouping, this._clubId);
    },


    /**
     * 删除成员
     * @private
     */
    _clickRemoveMember: function () {
        var self = this;
        showAlert('是否删除成员?', function () {
            network.send(2103, {
                cmd: ClubMsgCode.RemoveClubMember,
                club_id: self._clubId,
                obj_id: self._userData['uid'],
                name: self._userData['name'],
                head: self._userData['head']
            });
        }, function () {
        });
        this.close();
    },

    /**
     * 设置禁玩
     * @private
     */
    _clickJinWan: function () {
        network.send(2103, {cmd: 'BlackListAdd', club_id: this._clubId, uid: this._userData['uid']});
        this.close();
    },

    /**
     * 设置解禁
     * @private
     */
    _clickJieJin: function () {
        network.send(2103, {cmd: 'BlackListDel', club_id: this._clubId, uid: this._userData['uid']});
        this.close();
    },

    /**
     * 选择分组 回调
     * @param data
     * @private
     */
    _selectGroupHandler: function (data) {
        var optPos = this._userData['groupPos'];
        var optGroupUids = ClubDataManager.getClubGroupsInfo(this._clubId, optPos).uids;
        var targetGroupUids = ClubDataManager.getClubGroupsInfo(this._clubId, data.pos).uids;
        var targetPos = data.pos;

        var index = optGroupUids.indexOf(this._userData['uid']);
        optGroupUids.splice(index, 1);

        targetGroupUids.push(this._userData['uid']);


        var set_group = {};
        set_group[optPos] = optGroupUids;
        set_group[targetPos] = targetGroupUids;

        network.send(2103, {
            cmd: ClubMsgCode.AddClubGroupUids,
            club_id: this._clubId,
            set_group: set_group
        });
    },

    _addClubGroupUidsHandler: function (data) {
        this.close();
    },
});